package com.study.controller;

import com.study.pojo.resp.ResultMessage;
import com.study.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 商城模块控制器
 * @author Screncea
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    ShopService service;

    /**
     * 根据商品类型id查询商品信息集合
     *
     * @return 商品集合
     */
    @RequestMapping("/selectAllGoods")
    public ResultMessage selectAllGoods(){
//        String goodType =(String) map.get("goodType");
        return service.selectAllGoods();
    }

    /**
     * 查询商品分类
     * @return 分类集合
     */
    @RequestMapping("/selectShopType")
    public ResultMessage selectShopType(){
        return service.selectShopType();
    }

    /**
     * 根据商品信息查询商品信息
     * @param map goodId 商品id
     * @return 商品信息
     */
    @RequestMapping("/findGoodByGoodId")
    public ResultMessage findGoodByGoodId(@RequestBody Map map){
        String goodId = (String) map.get("goodId");
        return service.findGoodByGoodId(goodId);
    }

    /**
     * 判断用户积分是否足够
     * @param map 商品加积分
     * @return result结果集
     */
    @RequestMapping("/findScore")
    public ResultMessage findScore(@RequestBody Map map, HttpServletRequest request){
        //通过cookie获取用户id
        Cookie[] cookie = request.getCookies();
        String userId = null;
        for (Cookie cookie2 : cookie) {
            String name = cookie2.getName();
            if ("userId".equals(name)) {
                userId = cookie2.getValue();
                break;
            }
        }
        Integer obj =(Integer) map.get("totalMoney");

            return service.findScore(obj,userId);
    }

    /**
     * 添加订单
     * @param map 商品id
     * @param request 用户id
     * @return
     */
        @RequestMapping("/addOrder")
        public ResultMessage addOrder(@RequestBody Map map,HttpServletRequest request){
            String goodId = (String) map.get("goodId");
            //通过cookie获取用户id
            Cookie[] cookie = request.getCookies();
            String userId = null;
            for (Cookie cookie2 : cookie) {
                String name = cookie2.getName();
                if ("userId".equals(name)) {
                    userId = cookie2.getValue();
                    break;
                }
            }
            return service.addOrder(goodId,userId);
        }
    /**
     * 用户支付
     * @param map 用户输入密码
     * @param request 获取用户id
     * @return
     */
    @RequestMapping("/pay")
    public ResultMessage pay(@RequestBody Map map,HttpServletRequest request){
        String userPassword =(String) map.get("userPassword");
        String goodId =(String) map.get("goodId");
        String orderId = (String) map.get("orderId");
        //通过cookie获取用户id
        Cookie[] cookie = request.getCookies();
        String userId = null;
        for (Cookie cookie2 : cookie) {
            String name = cookie2.getName();
            if ("userId".equals(name)) {
                userId = cookie2.getValue();
                break;
            }
        }
        return service.pay(userPassword,userId,goodId,orderId);
    }

    /**
     * 查找支付成功订单的信息
     * @param map
     * @return
     */
    @RequestMapping("/findSuccessOrder")
    public ResultMessage findSuccessOrder(@RequestBody Map map ,HttpServletRequest request){
        String code = (String) map.get("code");
        return service.findSuccessOrder( code,request);
    }

}
